package Algorithms;

import giny.model.Node;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;

import javax.swing.JOptionPane;

import GUI.ResultsWindow;
import General.AdjacencyMatrix;
import General.DistanceMatrix;
import General.MembershipMatrix;

import cytoscape.CyNetwork;
import cytoscape.task.Task;
import cytoscape.task.TaskMonitor;
import cytoscape.view.CyNetworkView;

public class AlgorithmManager implements Task{

	
	private CentersSelectionAlgorithm centerSelectionAlgorithm;
	private ClusteringAlgorithm clusteringAlgorithm;
	private boolean extended;
	
	//parameters for clustering:
	private double m;
	private double eps;
	private int c,n;
	private CyNetwork graph;
	private CyNetworkView view;
	private Set nodeSet;
	
	private ArrayList<Node> array;
	
	public AlgorithmManager(boolean extended, int numClusters, double m, double epsillon, CyNetwork graph, CyNetworkView view, boolean onlySelected)
	{
		this.extended=extended;
		if (!extended)
			this.c=numClusters;
		this.m=m;
		this.view=view;
		this.eps=epsillon;
		this.graph=graph;
		array=new ArrayList<Node>();
		Iterator<Object> iter=graph.nodesIterator();
		if (onlySelected)
		{
			// add only selected nodes
			while (iter.hasNext())
			{
				Node node = (Node) iter.next();
				if (graph.isSelected(node))
					array.add(node);
			
			}
		}
		else
		{
			// add all nodes
			while (iter.hasNext())
			{
				Node node = (Node) iter.next();
				array.add(node);
			}
		}
		//nodeSet=graph.getSelectedNodes();
		this.n=array.size();
		 Collections.reverse(array); // this seems to return to nodes in reverse, so fix it!
		JOptionPane.showMessageDialog(null, "nodes array: \n"+array.toString());
		
	}
	
	@Override
	public String getTitle() {
		// TODO Auto-generated method stub
		return "Extended Fuzzy Clustering Plugin";
	}

	@Override
	public void halt() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void run() 
	{ // this method is the method that runs the algorithms 
		int[] centers;
		MembershipMatrix result=null;
		// Calculate Adjacency Matrix:
		AdjacencyMatrix a = new AdjacencyMatrix(graph, array);
		
		JOptionPane.showMessageDialog(null, "Adjacency Matrix: " + a.toString());
		
		// Calculate Distance Matrix:
		DistanceMatrix d = new DistanceMatrix(a);
		
		if (extended)
		{
			centerSelectionAlgorithm = new RandomCentersSelection(a);
			centers=centerSelectionAlgorithm.run();
			clusteringAlgorithm=new ExtendedFuzzyCMeans(a, d, m, eps, centers);
			result = clusteringAlgorithm.run();
		}
		else
		{
		
		}
		
		d=null;
		a=null;
		
		JOptionPane.showMessageDialog(null, "The result is: "+ result.toString());
		new ResultsWindow(result, array, graph, view);
		JOptionPane.showMessageDialog(null, "The method run() finished execution.");

		}

	@Override
	public void setTaskMonitor(TaskMonitor arg0)
			throws IllegalThreadStateException {
		// TODO Auto-generated method stub
		
	}

}
